package com.runjian.rundo.iot.agent.iec104.parser.frame.control.process;

import com.runjian.rundo.iot.agent.iec104.consts.Ti;
import com.runjian.rundo.iot.agent.iec104.parser.frame.DoublePointRemoteControl;
import com.runjian.rundo.iot.agent.iec104.parser.frame.InfoElement;
import com.runjian.rundo.iot.agent.iec104.parser.frame.control.base.YkControl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ByteUtil;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.Setter;

/**
 * C_DC_NA_1(46, "(遥控)双命令")
 *
 * @author CaoQingHua
 */
@Getter
@Setter
public class C_DC_NA_1 extends YkControl {

    @Override
    public void analysisInfo(byte[] msg) {
        this.addInfoElement(InfoElement.builder().address(ByteUtil.bytesToInt(new byte[] {msg[12], msg[13], msg[14], 0x00}))
            .value(new DoublePointRemoteControl(msg[15])).build());
    }

    @Override
    public void init() {
        super.init();
        // 类型
        this.setTi(Ti.C_DC_NA_1);

    }

    /**
     * 添加控制信息元素
     *
     * @param address 地址
     * @param value 数据
     */
    @Override
    public void addControlInfoElement(int address, Object value) {
        if (!(value instanceof Integer) || !(Integer.valueOf(value.toString()) >= 1 && Integer.valueOf(value.toString()) <= 2)) {
            Assert.isTrue(Boolean.FALSE, "值必须是整型数字,并且是（0，1，2）");
        }
        DoublePointRemoteControl doublePointRemoteControl = new DoublePointRemoteControl(true, Integer.valueOf(value.toString()));
        this.addInfoElement(InfoElement.builder().address(address).value(doublePointRemoteControl).build());
    }




}
